package net.uhuijia.zb.userlivevisitrecord.service.impl;

import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.vo.zb.UserLiveVisitRecordVo;
import net.uhuijia.zb.userlivevisitrecord.service.UserLiveVisitRecordService;
import net.uhuijia.zb.userzhubo.persistence.model.UserZhubo;
import net.uhuijia.zb.userzhubo.service.UserZhuboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.zb.userlivevisitrecord.persistence.dao.UserLiveVisitRecordDao;
import net.uhuijia.zb.userlivevisitrecord.persistence.model.UserLiveVisitRecord;
import net.uhuijia.zb.userlivevisitrecord.persistence.model.UserLiveVisitRecordExample;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class UserLiveVisitRecordServiceImpl
     extends CrudService<UserLiveVisitRecordDao,UserLiveVisitRecord,UserLiveVisitRecordExample>
        implements UserLiveVisitRecordService{
    @Autowired
    private UserLiveVisitRecordDao userLiveVisitRecordDao;
    @Autowired
    private UserZhuboService userZhuboService;
    /**
     * 查询用户多少条访问记录
     * @param userId 用户Id
     * @param length  条数
     * @return
     */
    @Override
    public List<UserLiveVisitRecord> findUserLiveVisitRecordByUserIdAndLength(String userId, Integer length) {
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("length",length);
        List<UserLiveVisitRecord> userLiveVisitRecords = this.userLiveVisitRecordDao.findUserLiveVisitRecordByUserIdAndLength(params);
        return userLiveVisitRecords;
    }

    /**
     * 首页访问记录
     * @param userMall 用户
     * @param length 条数
     * @return
     */
    @Override
    public List<UserLiveVisitRecordVo> indexVisitRecord(UserMall userMall, Integer length) {
        length = length ==null?10:length.intValue();
        List<UserLiveVisitRecord> userLiveVisitRecords = this.findUserLiveVisitRecordByUserIdAndLength(userMall.getId(),length);
        List<UserLiveVisitRecordVo> userLiveVisitRecordVos = new ArrayList<>();
        for(UserLiveVisitRecord userLiveVisitRecord : userLiveVisitRecords){
            UserLiveVisitRecordVo userLiveVisitRecordVo = this.userLiveVisitRecordWrapUserLiveVisitRecordVo(userLiveVisitRecord);
            userLiveVisitRecordVos.add(userLiveVisitRecordVo);
        }
        return userLiveVisitRecordVos;
    }

    /**
     * 将UserLiveVisitRecord 转换成UserLiveVisitRecordVo
     * @param userLiveVisitRecord
     * @return
     */
    public UserLiveVisitRecordVo userLiveVisitRecordWrapUserLiveVisitRecordVo(UserLiveVisitRecord userLiveVisitRecord){
        UserLiveVisitRecordVo userLiveVisitRecordVo = new UserLiveVisitRecordVo();
        UserZhubo userZhubo = this.userZhuboService.getUserZhuboByUserId(userLiveVisitRecord.getZbUserId());
        if(userZhubo!=null){
            userLiveVisitRecordVo.setHeadImage(userZhubo.getZbHeadImg());
            userLiveVisitRecordVo.setNickName(userZhubo.getZbName());
            userLiveVisitRecordVo.setUserId(userZhubo.getUserId());
        }
        return userLiveVisitRecordVo;
    }
}